package com.linxz.ui.spraycarview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;


public abstract class ICarPart {
        private int left;
        private int top;
        private int width;
        private int height;


       public ICarPart(int left, int top, int width, int height) {
          this.left = left;
          this.top = top;
          this.width=width;
          this.height=height;
      }

    public void draw(Context context, Canvas canvas, Paint paint,int res){

        Bitmap bitmap=BitmapUtil.decodeSampledBitmapFromResources(context.getResources(),res,width,height);
        // Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),res);
        System.out.println("###draw "+left+" "+top+" "+width+" "+height+"->"+getRes());
        canvas.drawBitmap(bitmap,left,top,paint);
        bitmap.recycle();
    }

      public void draw(Context context, Canvas canvas, Paint paint){
         draw(context,canvas,paint,getRes());
      }

    public int getLeft() {
        return left;
    }

    public void setLeft(int left) {
        this.left = left;
    }

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public abstract int getRes();



    public void adjustPosition(int viewWidth,int viewHeight){
        float baseWidth=750;
        float baseHeight=370;
        int left=Math.round((getLeft()/baseWidth*viewWidth));
        int top=Math.round(getTop()/baseHeight*viewHeight);
        int width=Math.round(getWidth()/baseWidth*viewWidth);
        int height=Math.round(getHeight()/baseHeight*viewHeight);
        setLeft(left);
        setTop(top);
        setWidth(width);
        setHeight(height);
    }

    public abstract boolean isShow();



    public boolean isInRect(int x,int y){
        Rect rect=new Rect(left,top,left+width,top+height);
       return rect.contains(x,y);
    }



}
